package com.javaPro.myProject.modules.article.service.impl;

import com.javaPro.myProject.common.co.filePath;
import com.javaPro.myProject.common.model.AjaxResult;
import com.javaPro.myProject.modules.article.dao.TArticleDao;
import com.javaPro.myProject.modules.article.entity.TArticle;
import com.javaPro.myProject.modules.article.service.TArticleService;
import com.javaPro.myProject.modules.comment.dao.TCommentDao;
import com.javaPro.myProject.modules.comment.entity.TComment;
import com.javaPro.myProject.modules.comment.service.TCommentService;
import com.javaPro.myProject.modules.files.dao.TFileDao;
import com.javaPro.myProject.modules.message.entity.Message;
import com.javaPro.myProject.modules.message.service.MessageService;
import com.javaPro.myProject.modules.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (TArticle)表服务实现类
 *
 * @author makejava
 * @since  07:51:43
 */
@Service("tArticleService")
public class TArticleServiceImpl implements TArticleService {
    @Resource
    private TArticleDao tArticleDao;
    @Resource
    private MessageService messageService;
    @Resource
    private TCommentService commentService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private TFileDao fileDao;
    @Resource
    private TCommentDao commentDao;
    @Resource
    private filePath filePath;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TArticle queryById(Integer id,String applyer) {
        TArticle tArticle = this.tArticleDao.queryById(id);
        //设置讨论内容，分段展示
        String content = tArticle.getContent();
        //遇到 换行符 \n ， 就另起一行
        content = "<p>" +  content.replace("\n","</p><p>") + "</p>";
        tArticle.setContent(content);

        //评论区------------
        TComment param = new TComment();
        param.setArticleId(id);
        param.setOriginFlag("1");
        List<TComment> tComments = commentService.queryByPage(param);//找到所有的楼主评论
//        if (!"1".equals(applyer)){
//            if (!applyer.equals( String.valueOf(tArticle.getAuthorId()))){//如果不是作者，评论区就要进行过滤，只能看到以 '当前登录人' 为楼主的评论
//                tComments = tComments.stream().filter(c -> applyer.equals(String.valueOf(c.getLandlordId()))).collect(Collectors.toList());
//            }
//        }

        for (TComment item : tComments) {
            Integer landlordId = item.getLandlordId();//楼主id
            TComment cParam = new TComment();
            cParam.setLandlordId(landlordId);//楼主id
            cParam.setArticleId(id);
            cParam.setOriginFlag("0");
            cParam.setCreateTime(item.getCreateTime());//这一步是为日了筛选  比楼主评论时间晚的回复！
            List<TComment> childComment = commentService.queryByPage(cParam);//找到楼主所有的回复
            item.setChildEvaluate(childComment);
        }
        tArticle.setEvaluateList(tComments);
        //---------------------

        //查看详情意味着阅读量+1，下面是浏览+1操作
        TArticle updateParam = new TArticle();
        updateParam.setId(id);
        updateParam.setReadNum(1);

        int update = tArticleDao.update(updateParam);


        //-------
        return tArticle;
    }

    /**
     * 分页查询
     *
     * @param tArticle 筛选条件
     * @return 查询结果
     */
    @Override
    public List<TArticle> queryByPage(TArticle tArticle) {
        if ("user".equals(tArticle.getApplyer())){
            tArticle.setStatus("1");
        }
        List<TArticle> tArticles = this.tArticleDao.queryAllByLimit(tArticle);
        tArticles.forEach(tArticle1 -> tArticle1.setContent(tArticle1.getContent().substring(0,(Math.min(tArticle1.getContent().length(), 50))) + "......"));
        return tArticles;
    }

    /**
     * 新增数据
     *
     * @param tArticle 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(TArticle tArticle) {
        int insert = this.tArticleDao.insert(tArticle);
        return insert;
    }

    /**
     * 修改数据
     *
     * @param tArticle 实例对象
     * @return 实例对象
     */
    @Override
    public int update(TArticle tArticle) {
        int update = this.tArticleDao.update(tArticle);
//        //驳回讨论,要给用户发提醒
//        if (tArticle.getStatus().equals("2")) {
//            TArticle t = tArticleDao.queryById(tArticle.getId());
//            Message message = new Message();
//            message.setUserId(t.getAuthorId());
//            message.setStatus("1");
//            message.setMsg("尊敬的用户您好，您的讨论: '"+t.getTitle()+"' 被管理员驳回，驳回原因："+tArticle.getCause());
//            message.setCause(tArticle.getCause());
//            messageService.insert(message);
//        }
        return update;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tArticleDao.deleteById(id) > 0;
    }


}
